package com.goods.controller.business;

import com.goods.business.service.ConsumerService;
import com.goods.business.service.OutStockService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Consumer;
import com.goods.common.model.business.OutStock;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ConsumerVO;
import com.goods.common.vo.business.OutStockDetailVO;
import com.goods.common.vo.business.OutStockVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Api(tags = "业务模块-发放记录相关接口")
@RestController
@RequestMapping("/business/outStock")
public class OutStockController {

  @Autowired
  private OutStockService outStockService;

  @Autowired
  private OutStockService outStockService;

  @Autowired
  private ConsumerService consumerService;

  @ApiOperation(value = "数据分页列表展示",notes = "用户点击左侧发放记录菜单时，右侧展示出库单分页列表")
  @GetMapping("/findOutStockList")
  public ResponseBean findOutStockList(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                                       @RequestParam(value = "pageSize")Integer pageSize,
                                       OutStockVO outStockVO){
    PageVO<OutStockVO> outStockVOPageVO = outStockService.findOutStockList(pageNum,pageSize,outStockVO);
    return ResponseBean.success(outStockVOPageVO);
  }

  @ApiOperation(value = "发放明细",notes = "用户选中一条记录点击明细时，展示该出库单对应的发放物资明细列表")
  @GetMapping("/detail/{id}")
  public ResponseBean detail(@PathVariable Long id,
                             @RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
                             @RequestParam(value = "pageSize",defaultValue = "3")Integer pageSize) throws BusinessException {
    OutStockDetailVO outStockDetailVO = outStockService.detail(id,pageNum,pageSize);
    return ResponseBean.success(outStockDetailVO);
  }

  @ApiOperation("提交发放单")
  @PostMapping("/addOutStock")
  public ResponseBean addOutStock(@RequestBody @Validated OutStockVO outStockVO) throws BusinessException {
    if (outStockVO.getConsumerId()==null){
      ConsumerVO consumerVO = new ConsumerVO();
      BeanUtils.copyProperties(outStockVO,consumerVO);
      if ("".equals(consumerVO.getName())||consumerVO.getName()==null){
        throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"物资去向名不能为空");
      }
      if("".equals(consumerVO.getContact())||consumerVO.getContact()==null){
        throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"联系人不能为空");
      }
      if("".equals(consumerVO.getAddress())||consumerVO.getAddress()==null){
        throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"地址不能为空");
      }
      if("".equals(consumerVO.getPhone())||consumerVO.getPhone()==null){
        throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"联系方式不能为空");
      }
      if(consumerVO.getSort()==null){
        throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"排序不能为空");
      }
      Consumer consumer = consumerService.add(consumerVO);
      outStockVO.setConsumerId(consumer.getId());
    }
    outStockService.addOutStock(outStockVO);
    return ResponseBean.success();
  }
}
